package com.mvpdemo.mvp;

import android.os.Handler;

/**
 * 连接数据操作和界面的桥梁
 */

public class UserLoginPresenterImpl implements IUserLoginPresenter, IUserModel.onLoginListener {

    private IUserLoginView mIUserLoginView;
    private IUserModel mIUserModel;

    private Handler handler = new Handler();

    public UserLoginPresenterImpl(IUserLoginView mIUserLoginView) {
        this.mIUserLoginView = mIUserLoginView;
        mIUserModel = new IUserModelImp(this);
    }

    @Override
    public void doLogin() {
        String userName = mIUserLoginView.getUserName();
        String passWord = mIUserLoginView.getPassWord();
        mIUserLoginView.showLoading();
        mIUserModel.onLogin(userName, passWord, this);
    }

    @Override
    public void clear() {
        mIUserLoginView.clearUserName();
        mIUserLoginView.clearPwd();
    }

    @Override
    public void loginSuccess() {
        handler.post(new Runnable() {
            @Override
            public void run() {
                mIUserLoginView.hiddenLoading();
                mIUserLoginView.toMainActivity();
            }
        });
    }

    @Override
    public void loginFailed() {
        handler.post(new Runnable() {
            @Override
            public void run() {
                mIUserLoginView.hiddenLoading();
                mIUserLoginView.showLoginFailed();
            }
        });
    }
}
